﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using YArchitech.DataModel;
using YArchitech.LIB;
using HYFamilyLibDataManager;

namespace YArchitech.HVAC
{
	public partial class DuctFittingSetForm : System.Windows.Forms.Form
	{
		public DuctFittingSetForm(Document doc)
		{
			this.InitializeComponent();
			//base.Icon = Resource.YArchitechIcon;
			this.doc = doc;
			this.hvacDb = new HVACDataReader(doc);
		}

		private void DuctFittingSetForm_Load(object sender, EventArgs e)
		{
			this.progressBar1.Visible = false;
			this.GetFamilyInstance();
			this.initParameterSetting();
		}

		private void GetFamilyInstance()
		{
			this.initComboBoxItem(this.hycmbElbowRect);
			this.hycmbElbowRect.Items.AddRange(this.hvacDb.GetFamilyArray("矩形弯头"));
			this.initComboBoxItem(this.hycmbTeeRect);
			this.hycmbTeeRect.Items.AddRange(this.hvacDb.GetFamilyArray("矩形三通"));
			this.initComboBoxItem(this.hycmbCrossRect);
			this.hycmbCrossRect.Items.AddRange(this.hvacDb.GetFamilyArray("矩形四通"));
			this.initComboBoxItem(this.hycmbTransRect);
			this.hycmbTransRect.Items.AddRange(this.hvacDb.GetFamilyArray("矩形变径"));
			this.initComboBoxItem(this.hycmbTakeOffRect);
			this.hycmbTakeOffRect.Items.AddRange(this.hvacDb.GetFamilyArray("矩形接头"));
			this.initComboBoxItem(this.hycmbTydfRect);
			this.hycmbTydfRect.Items.AddRange(this.hvacDb.GetFamilyArray("风管天圆地方"));
			this.initComboBoxItem(this.hycmbElbowRound);
			this.hycmbElbowRound.Items.AddRange(this.hvacDb.GetFamilyArray("圆形弯头"));
			this.initComboBoxItem(this.hycmbTeeRound);
			this.hycmbTeeRound.Items.AddRange(this.hvacDb.GetFamilyArray("圆形三通"));
			this.initComboBoxItem(this.hycmbCrossRound);
			this.hycmbCrossRound.Items.AddRange(this.hvacDb.GetFamilyArray("圆形四通"));
			this.initComboBoxItem(this.hycmbTransRound);
			this.hycmbTransRound.Items.AddRange(this.hvacDb.GetFamilyArray("圆形变径"));
			this.initComboBoxItem(this.hycmbTydfRound);
			this.hycmbTydfRound.Items.AddRange(this.hvacDb.GetFamilyArray("风管天圆地方"));
		}

		private void initComboBoxItem(ComboBox cb)
		{
			cb.DisplayMember = "Name";
			cb.ValueMember = "Value";
		}

		private void initParameterSetting()
		{
			try
			{
				DuctFittingSet ductFittingSetText = this.userSetting.GetDuctFittingSetText();
				this.hycmbElbowRect.Text = ductFittingSetText.RectFitting.Elbow;
				this.hycmbTeeRect.Text = ductFittingSetText.RectFitting.Tee;
				this.hycmbCrossRect.Text = ductFittingSetText.RectFitting.Cross;
				this.hycmbTakeOffRect.Text = ductFittingSetText.RectFitting.TakeOff;
				this.hycmbTransRect.Text = ductFittingSetText.RectFitting.Transition;
				this.hycmbTydfRect.Text = ductFittingSetText.RectFitting.TYDF;
				this.hycmbElbowRound.Text = ductFittingSetText.RoundFitting.Elbow;
				this.hycmbTeeRound.Text = ductFittingSetText.RoundFitting.Tee;
				this.hycmbCrossRound.Text = ductFittingSetText.RoundFitting.Cross;
				this.hycmbTransRound.Text = ductFittingSetText.RoundFitting.Transition;
				this.hycmbTydfRound.Text = ductFittingSetText.RoundFitting.TYDF;
			}
			catch (Exception)
			{
			}
		}

		private void UserSettingWrite()
		{
			try
			{
				DuctFittingSet ductFittingSet = new DuctFittingSet();
				ductFittingSet.RectFitting.Elbow = this.hycmbElbowRect.Text;
				ductFittingSet.RectFitting.Tee = this.hycmbTeeRect.Text;
				ductFittingSet.RectFitting.Cross = this.hycmbCrossRect.Text;
				ductFittingSet.RectFitting.TakeOff = this.hycmbTakeOffRect.Text;
				ductFittingSet.RectFitting.Transition = this.hycmbTransRect.Text;
				ductFittingSet.RectFitting.TYDF = this.hycmbTydfRect.Text;
				ductFittingSet.RoundFitting.Elbow = this.hycmbElbowRound.Text;
				ductFittingSet.RoundFitting.Tee = this.hycmbTeeRound.Text;
				ductFittingSet.RoundFitting.Cross = this.hycmbCrossRound.Text;
				ductFittingSet.RoundFitting.Transition = this.hycmbTransRound.Text;
				ductFittingSet.RoundFitting.TYDF = this.hycmbTydfRound.Text;
				this.userSetting.WriteDuctFittingSetText(ductFittingSet);
			}
			catch (Exception)
			{
			}
		}

		private void hybtnOK_Click(object sender, EventArgs e)
		{
			this.UserSettingWrite();
			this.FamilyLoad();
			base.DialogResult = DialogResult.OK;
		}

		private void FamilyLoad()
		{
			this.progressBar1.Visible = true;
			this.progressBar1.Value = 0;
			DBFamilyListItem dbfamilyListItem = this.hycmbElbowRect.SelectedItem as DBFamilyListItem;
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 9;
			dbfamilyListItem = (this.hycmbTeeRect.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 18;
			dbfamilyListItem = (this.hycmbCrossRect.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 27;
			dbfamilyListItem = (this.hycmbTransRect.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 36;
			dbfamilyListItem = (this.hycmbTakeOffRect.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 45;
			dbfamilyListItem = (this.hycmbTydfRect.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 54;
			dbfamilyListItem = (this.hycmbElbowRound.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 63;
			dbfamilyListItem = (this.hycmbTeeRound.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 72;
			dbfamilyListItem = (this.hycmbCrossRound.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 81;
			dbfamilyListItem = (this.hycmbTransRound.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 90;
			dbfamilyListItem = (this.hycmbTydfRound.SelectedItem as DBFamilyListItem);
			this.hvacDb.FamilyLoad(dbfamilyListItem.Family as DbFamily, this.doc);
			this.progressBar1.Value = 100;
		}

		private void hybtnCancel_Click(object sender, EventArgs e)
		{
			base.DialogResult = DialogResult.Cancel;
		}

		private HVACUserSetting userSetting = new HVACUserSetting();

		private HVACDataReader hvacDb;

		private Document doc;
	}
}
